package cn.fx.basic.controller;

import cn.fx.basic.exception.BusinessException;
import cn.fx.basic.service.IVerifyCodeService;
import cn.fx.basic.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 图形验证码接口
 */
@RequestMapping("/verifyCode")
@RestController
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public JsonResult getVerifyCode(@PathVariable("key") String key){

        try {
            String baseStr = verifyCodeService.getVerifyCode(key);
            //把返回的数据封装到JsonResult,给页面
            return  JsonResult.me().setResultObj(baseStr);
        } catch(BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());

        }catch(Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("业务繁忙,请稍后重试");
        }
    }

    /**
     * 短信验证码  接收参数: dto
     *                    map接收
     * @return
     */
    @PostMapping("/smsCode")
    public JsonResult smsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.smsCode(map);
            return JsonResult.me();
        } catch(BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());

        }catch(Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("业务繁忙,请稍后重试");
        }
    }

    /**
     * 绑定验证码
     * @param map
     * @return
     */
    @PostMapping("/binderSmsCode")
    public JsonResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.binderSmsCode(map);
            return JsonResult.me();
        } catch(BusinessException e) {
            e.printStackTrace();
            return JsonResult.me().setMsg(e.getMessage());

        }catch(Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("业务繁忙,请稍后重试");
        }
    }
}
